#include "mainwindow.h"
#include "ui_mainwindow.h"

QMap<int, MiniObject*> Global::miniliste;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(saveSettings()));

    /** Datenbank initialisieren **/
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("miniplaner.db");
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery("PRAGMA encoding = 'UTF-8'; ", db);
        QSqlQuery("PRAGMA foreign_keys = true;", db);
        QSqlQuery("CREATE TABLE IF NOT EXISTS ministranten (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, vorname TEXT, anschrift TEXT, plz TEXT, ort TEXT, telefon TEXT, telefax TEXT, email TEXT);", db);
    }

    for ( int i=0; i < ui->tabWidget->count(); i++ )
        ui->tabWidget->setCurrentIndex(i);

    loadSettings();
    loadMinistranten();
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::saveSettings()
{
    QSettings settings;
    settings.setValue("mainwindow/maximized", isMaximized());
    if ( isMaximized() )
        showNormal();
    settings.setValue("mainwindow/size", size());
    settings.setValue("mainwindow/pos", pos());
    if ( settings.value("mainwindow/maximized", false).toBool() )
        showMaximized();

    settings.setValue("tabWidget", ui->tabWidget->currentIndex());
}

void MainWindow::loadSettings()
{
    QSettings settings("FHsoft", "MiniPlaner");
    resize(settings.value("mainwindow/size", QSize(800, 600)).toSize());
    move(settings.value("mainwindow/pos", QPoint(200, 200)).toPoint());
    if ( settings.value("mainwindow/maximized", false).toBool() )
        showMaximized();

    ui->tabWidget->setCurrentIndex( settings.value("tabWidget", 0).toInt() );
}

void MainWindow::loadMinistranten()
{
    if ( db.isOpen() || db.open() )
    {
        QSqlQuery q("SELECT id FROM ministranten ORDER BY id ASC", db);
        while ( q.next() )
        {
            Global::miniliste.insert(q.value(0).toInt(), new MiniObject(q.value(0).toInt()));
        }
        q.finish();

        ui->ministranten_listWidget->clear();
        foreach (MiniObject *m, Global::miniliste)
        {
            ui->ministranten_listWidget->addItem(m->getListWidgetItem());
        }
    }
}


void MainWindow::on_tabWidget_currentChanged(int index)
{
    ui->termineToolBar->setVisible( (index==0)?true:false );
    ui->ministrantenToolBar->setVisible( (index==1)?true:false );
}

void MainWindow::on_ministranten_listWidget_itemSelectionChanged()
{
    if ( ui->ministranten_editWidget->getDataChanged() && QMessageBox::question(this, tr("Ministrant speichern"), tr("Sollen die Änderungen gespeichert werden?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes )
        ui->ministranten_editWidget->on_actionMinistrant_speichern_triggered();

    if ( ui->ministranten_listWidget->selectedItems().count() == 1 )
    {
        ui->ministranten_editWidget->loadMini( ui->ministranten_listWidget->selectedItems().at(0)->data(Qt::UserRole).toInt() );
    }

    ui->actionMinistrant_loeschen->setEnabled( ui->ministranten_listWidget->selectedItems().isEmpty()?false:true );
}

void MainWindow::on_actionNeuer_Ministrant_triggered()
{
    MiniObject *m = new MiniObject(-1);
    Global::miniliste.insert(m->getId(), m);
    ui->ministranten_listWidget->addItem(m->getListWidgetItem());
    ui->ministranten_listWidget->sortItems();
    ui->ministranten_listWidget->setCurrentItem(m->getListWidgetItem());
}

void MainWindow::on_actionMinistrant_loeschen_triggered()
{
    // TODO: Prüfen, ob Ministrant eingeteilt ist, dann nur deaktivieren
    foreach ( QListWidgetItem *i, ui->ministranten_listWidget->selectedItems() )
    {
        int id = i->data(Qt::UserRole).toInt();
        if ( Global::miniliste[id]->deleteMini() )
            Global::miniliste.remove(id);
    }
}
